package cyou.breathe.blog.article.controller;

import cn.dev33.satoken.stp.StpUtil;
import cyou.breathe.blog.api.article.model.ArticleVO;
import cyou.breathe.blog.api.article.request.ArticleCreateRequest;
import cyou.breathe.blog.api.article.request.ArticleDeleteRequest;
import cyou.breathe.blog.api.article.request.ArticleQueryRequest;
import cyou.breathe.blog.api.article.request.ArticleUpdateRequest;
import cyou.breathe.blog.api.article.response.ArticleCreateResponse;
import cyou.breathe.blog.api.article.response.ArticleDeleteResponse;
import cyou.breathe.blog.api.article.response.ArticleUpdateResponse;
import cyou.breathe.blog.article.domain.entity.Article;
import cyou.breathe.blog.article.domain.entity.convertor.ArticleConvertor;
import cyou.breathe.blog.article.domain.service.ArticleService;
import cyou.breathe.blog.article.param.CreateArticleParam;
import cyou.breathe.blog.article.param.UpdateArticleParam;
import cyou.breathe.blog.web.vo.Result;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: breathe
 * @createTime: 2025-09-01
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @PostMapping("/createArticle")
    public Result<Long> createArticle(@Valid @RequestBody CreateArticleParam createArticleParam) {
        long authorId = StpUtil.getLoginIdAsLong();

        ArticleCreateRequest articleCreateRequest = new ArticleCreateRequest();
        articleCreateRequest.setAuthorId(authorId);
        articleCreateRequest.setTags(createArticleParam.getTags());
        articleCreateRequest.setTitle(createArticleParam.getTitle());
        articleCreateRequest.setState(createArticleParam.getState());
        articleCreateRequest.setContent(createArticleParam.getContent());
        articleCreateRequest.setFolderId((createArticleParam.getFolderId()));

        ArticleCreateResponse response = articleService.createArticle(articleCreateRequest);

        if (response.getSuccess()) {
            return Result.success(response.getArticleId());
        }

        return Result.error(response.getErrorCode());
    }

    @PutMapping("/updateArticle")
    public Result<Long> updateArticle(@Valid @RequestBody UpdateArticleParam updateArticleParam) {
        long userId = StpUtil.getLoginIdAsLong();

        ArticleUpdateRequest request = new ArticleUpdateRequest();
        request.setAuthorId(userId);
        request.setArticleId(updateArticleParam.getArticleId());
        request.setFolderId(updateArticleParam.getFolderId());
        request.setTags(updateArticleParam.getTags());
        request.setTitle(updateArticleParam.getTitle());
        request.setContent(updateArticleParam.getContent());

        ArticleUpdateResponse response = articleService.updateArticle(request);

        if (!response.getSuccess()) {
            return Result.error(response.getErrorCode());
        }

        return Result.success(response.getArticleId());
    }

    @DeleteMapping("/deleteArticle")
    public Result<Long> deleteArticle(@NotNull @RequestParam("articleId") Long articleId) {
        long userId = StpUtil.getLoginIdAsLong();
        ArticleDeleteRequest request = new ArticleDeleteRequest();
        request.setAuthorId(userId);
        request.setArticleId(articleId);
        ArticleDeleteResponse response = articleService.deleteArticle(request);
        if (!response.getSuccess()) {
            return Result.error(response.getErrorCode());
        }

        return Result.success(response.getArticleId());
    }

    @GetMapping("/queryArticlesByFolderId")
    public Result<List<ArticleVO>> queryArticles(@NotNull Long folderId) {
        long userId = StpUtil.getLoginIdAsLong();

        List<Article> articles = articleService.queryArticlesByAuthorIdAndFolderId(folderId, userId);

        return Result.success(articles.stream().map(ArticleConvertor.INSTANCE::mapToVO).toList());
    }
}
